package org.study.stream;

import lombok.*;

import java.util.*;

/**
 * @author Administrator
 */
public class StreamTest03 {
    public static void main(String[] args) {
        removeDuplicateUser(users);
        removeDuplicateUserByLambda(users);
        removeDuplicateUserByLambda2(users);
    }

    private static List<User> users;

    static {
        users = Arrays.asList(
                new User("张三", 1000),
                new User("张三", 1100),
                new User("张三", 1200),
                new User("李四", 1000),
                new User("李四", 1100),
                new User("王五", 2500),
                new User("赵六", 1800)
        );
    }

    public static void removeDuplicateUser(List<User> users) {
        System.out.println("=====>>>>>传统方式去重……");
        Set<User> set = new TreeSet<>(new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                return o1.getUsername().compareTo(o2.getUsername());
            }
        });
        set.addAll(users);
        new ArrayList<>(set).forEach(System.out::println);
    }

    public static void removeDuplicateUserByLambda(List<User> users) {
        System.out.println("=====>>>>>Lambda 方式去重……");
        Set<User> set = new TreeSet<>((user1, user2) -> user1.getUsername().compareTo(user2.getUsername()));
        set.addAll(users);
        new ArrayList<>(set).forEach(System.out::println);
    }

    public static void removeDuplicateUserByLambda2(List<User> users) {
        System.out.println("=====>>>>>Lambda 方式去重2……");
        Set<User> set = new TreeSet<>(Comparator.comparing(User::getUsername));
        set.addAll(users);
        new ArrayList<>(set).forEach(System.out::println);
    }

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    static class User {
        private String username;
        private int money;
    }
}
